package com.yjd.bi.gift.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.report.util.TimeDimensionUtil;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.bi.gift.model.GiftReceiveModel;
import com.yjd.bi.gift.service.IGiftReceiveService;
import com.yjd.bi.gift.dao.IGiftReceiveDao;

/**
 * <b>description</b>：微信分享红包业务实现<br>
 * <b>time</b>：2016-12-16 13:27:40 <br>
 * <b>author</b>：  ready likun_557@163.com
 */
@Service("giftReceiveService")
public class GiftReceiveServiceImpl extends BaseServiceImpl implements IGiftReceiveService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public GiftReceiveModel insert(GiftReceiveModel model) throws Exception {
		return this.giftReceiveDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(GiftReceiveModel model) throws Exception {
		return this.giftReceiveDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param gift_receive_id
	 * @return
	 * @throws Exception
	 */
	public int delete(long gift_receive_id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("gift_receive_id", gift_receive_id);
		return this.giftReceiveDao.deleteModel(paramMap, true);
	}

	@Resource
	private IGiftReceiveDao giftReceiveDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.giftReceiveDao;
	}

	@Override
	public EChartsModel getDates(TimeType timeType, Long timeFrom, String field) throws Exception {
		EChartsModel ec = new EChartsModel();
		ec.setxAxis(FrameUtil.newArrayList(null));
		ec.setLegend(FrameUtil.newArrayList(getFormateX(timeFrom)));
		ec.addSeries(FrameUtil.newArrayList(null), getFormateX(timeFrom));
		List<List<Long>> searchParam = TimeDimensionUtil.getSearchRanges(
				timeType, timeFrom);
		for (int i = searchParam.size() - 1; i >= 0; i--) {
			ec.getXAxis().add(DateUtil.getFormateStringByTimeDimention(searchParam.get(i).get(1), "yyyy-MM-dd"));
			Integer sum=this.giftReceiveDao.getSummary(FrameUtil.newHashMap(
					"start",DateUtil.dateStringToTimestamp(searchParam.get(i).get(0).toString(),
						DateUtil.PATTERN_yyyyMMdd),
					"end",DateUtil.dateStringToTimestamp( searchParam.get(i).get(1).toString(),
							DateUtil.PATTERN_yyyyMMdd)+86400,"propertyName", field));
				ec.getSeries().get(0).getData() .add( sum.toString() );
			}
		return ec;
	}

	public String getFormateX(Long value) {
		return DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(
				value + "", DateUtil.PATTERN_yyyyMMdd),
				DateUtil.PATTERN_yyyy_MM_dd);
	}
}
